package com.kewill.jira.quartz;

import com.kewill.jira.common.DaoAnnotation;
import com.kewill.jira.common.JiraAttribute;
import com.kewill.jira.dao.IIssueDao;
import com.kewill.jira.dao.IssueDaoImpl;
import com.kewill.jira.model.Issue;
import com.kewill.jira.servlet.IssueBaseServlet;
import com.kewill.jira.util.JiraSyncUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by YanJun on 2016/7/13.
 */
public class SyncIssue extends IssueBaseServlet implements Job {
    private Logger syncLog = LoggerFactory.getLogger(SyncIssue.class);
    private IIssueDao issueDao;

    @DaoAnnotation("IssueDaoImpl")
    public void setIssueDao(IIssueDao issueDao) {
        this.issueDao = issueDao;
    }
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JiraSyncUtil.init(JiraAttribute.JIRA_QUERY_ID,JiraAttribute.JIRA_USERNAME,JiraAttribute.JIRA_PASSWORD);
        List<Issue> issueList = JiraSyncUtil.analysisIssueXml();
        backupIssues();
        if(issueList.size()>0){
            for(Issue issue:issueList){
                issueDao.insertIssue(issue);
                syncLog.debug("cloud jira sync --> " + issue.getKeyId() + " " + issue.getTitle());
            }
        }
    }

    private void backupIssues(){
        issueDao.deleteAllIssues("issue_bak");
        issueDao.backupIssue();
        issueDao.deleteAllIssues("issue");
    }
}
